package com.bigdata.service.impl;

import com.bigdata.dao.NetworkMapper;
import com.bigdata.entity.pojo.OperatorData;
import com.bigdata.entity.pojo.ProvinceTraffic;
import com.bigdata.entity.vo.AppFlowTrackingVO;
import com.bigdata.entity.pojo.NetworkTraffic;
import com.bigdata.entity.vo.ProvinceTrafficQualityVO;
import com.bigdata.service.NetworkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class NetworkServiceImpl implements NetworkService {
    @Autowired
    private NetworkMapper networkMapper;
    public AppFlowTrackingVO NetworkFlowTracking(){

        AppFlowTrackingVO appFlowTrackingVO=new AppFlowTrackingVO();

        List<List<BigInteger>>traffic=new ArrayList<>();
        List<Long>time=new ArrayList<>();
        List<String>networkName=new ArrayList<>();
        networkName.add("ALL");
        networkName.add("CMCC");
        networkName.add("CTCC");
        networkName.add("CUCC");
        for( int i=2010;i<=2021;i++){
            Long temp=Long.valueOf(i);
          time.add(temp);
        }
        LocalDateTime start = LocalDateTime.parse("2010-01-01T00:00:00");
        LocalDateTime end = LocalDateTime.parse("2021-12-31T23:59:59");
        // 定义格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHH");
        // 格式化 LocalDateTime 为字符串
        String startString = start.format(formatter);
        String endString =end.format(formatter);
        Map<String, Map<Long,BigInteger>>map=new HashMap<>();
        BigInteger start1=new BigInteger(startString);
        BigInteger end1=new BigInteger(endString);
        List<NetworkTraffic>data=networkMapper.NetworkFlowTracking(networkName,start1,end1);
        for(NetworkTraffic networkTraffic:data){
            Map<Long,BigInteger> temp=map.getOrDefault(networkTraffic.getNetworkName(),new HashMap<>());
            temp.put(networkTraffic.getYear(),networkTraffic.getSpeed());
            map.put(networkTraffic.getNetworkName(),temp);
        }

        List<BigInteger>cmcc=new ArrayList<>();
        List<BigInteger>cucc=new ArrayList<>();
        List<BigInteger>ctcc=new ArrayList<>();
        List<BigInteger>all=new ArrayList<>();
        for(int k=1;k<4;k++) {
            String name=networkName.get(k);
            Map<Long, BigInteger> temp = map.get(name);
            System.out.println(temp);
            for (Long a : time) {

                BigInteger temp1 = temp.getOrDefault(a, BigInteger.ZERO);

                if(name.equals("CMCC")){
                cmcc.add(temp1);}
                else if(name.equals("CUCC")){
                    cucc.add(temp1);
                }
                else if(name.equals("CTCC")){
                    ctcc.add(temp1);
                }
            }
        }
            for(int i=0;i<12;i++){
                all.add(cmcc.get(i).add(cucc.get(i).add(ctcc.get(i))));
            }


       List<List<BigInteger>>speed=new ArrayList<>();
            speed.add(all);
        speed.add(cmcc);
        speed.add(ctcc);
        speed.add(cucc);

       appFlowTrackingVO.setLegends(networkName);
       List<String>timeToString=time.stream().map(String::valueOf).collect(Collectors.toList());
        appFlowTrackingVO.setXAxisData(timeToString);
        appFlowTrackingVO.setSeriesData(speed);
        return appFlowTrackingVO;
    }

    @Override
    public ProvinceTrafficQualityVO ProvinceTrafficQuality() {
        Long amount=5L;
        List<String>provinceName=networkMapper.getProvinceTop(amount);
List<ProvinceTraffic>provinceTraffic=networkMapper.getProvinceTraffic(provinceName);
Map<String,Map<String,Double>>map=new HashMap<>();
for(ProvinceTraffic p:provinceTraffic){
    Map<String,Double>temp=map.getOrDefault(p.getProvince(),new HashMap<>());
    temp.put(p.getNetworkName(),p.getSpeed());
    map.put(p.getProvince(),temp);
}
List<Double>cmcc=new ArrayList<>();
List<Double>ctcc=new ArrayList<>();
List<Double>cucc=new ArrayList<>();
for(String name:provinceName){
    Map<String,Double>temp=map.get(name);
    cmcc.add(temp.getOrDefault("CMCC",0.0));
    ctcc.add(temp.getOrDefault("CTCC",0.0));
    cucc.add(temp.getOrDefault("CUCC",0.0));
}
OperatorData operatorData=new OperatorData();
operatorData.setCmcc(cmcc);
operatorData.setCtcc(ctcc);
operatorData.setCucc(cucc);
ProvinceTrafficQualityVO provinceTrafficQualityVO=new ProvinceTrafficQualityVO();
provinceTrafficQualityVO.setLocations(provinceName);
provinceTrafficQualityVO.setOperatorData(operatorData);
        return provinceTrafficQualityVO;
    }
}
